// index.ts
import { CameraOrbitControlSchema } from 'XrFrame/components'
import {saveVxUser,downloadAvatar,uploadAvatar} from '../../api/api'
import { STORAGE_KEY } from '../../api/config'
import { LENGYU_SUCCESS_CODE } from '../../utils/request'
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    userInfo: {
      avatarUrl: wx.getStorageSync(STORAGE_KEY.AVATAR)?wx.getStorageSync(STORAGE_KEY.AVATAR):defaultAvatarUrl,
      nickName: wx.getStorageSync(STORAGE_KEY.NICK_NAME)?wx.getStorageSync(STORAGE_KEY.NICK_NAME):"",
    },
    hasUserInfo: false,
    error:""
  },
  
  methods: {
    onLoad: function() {
      this.setData({
        "userInfo.avatarUrl": wx.getStorageSync(STORAGE_KEY.AVATAR)?wx.getStorageSync(STORAGE_KEY.AVATAR):defaultAvatarUrl,
        "userInfo.nickName":wx.getStorageSync(STORAGE_KEY.NICK_NAME)?wx.getStorageSync(STORAGE_KEY.NICK_NAME):"勇士大哥",
      })
    },
    async onChooseAvatar(e: any) {
      console.log(e);
      let result1 = await uploadAvatar(e.detail.avatarUrl,{})
      console.log("文件上传成功",result1)
      this.setData({
        "userInfo.avatarUrl": result1.data,
      })
    },

    onInputChange(e: any) {
      const nickName = e.detail.value
      this.setData({
        "userInfo.nickName": nickName,
      })
    },
    async submit(){
      console.log("确认按钮点击事件")
      if(!this.data.userInfo.nickName){
        this.setData({
          "error":"请选择昵称"
        })
       
        return
      }
      if(!this.data.userInfo.avatarUrl){
        this.setData({
          "error":"请选择头像"
        })
        
        return 
      }
     
      let res = await saveVxUser(this.data.userInfo.nickName,this.data.userInfo.avatarUrl);
      if(res.code == LENGYU_SUCCESS_CODE){
        wx.setStorageSync(STORAGE_KEY.AVATAR,this.data.userInfo.avatarUrl);
        wx.setStorageSync(STORAGE_KEY.NICK_NAME,this.data.userInfo.nickName);
        console.info("个人信息保存完成")
        this.nextPage();
      }
      else{

      }
    },
    nextPage(){
      wx.switchTab({
        url: '../self/self',
      })
    }
  },
})
